package com.rem.concurrency.juc.my8Bq;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * 同步队列
 * 没有容量，SynchronousQueue 不存储元素
 * 进去一个元素，必须等待取出来之后，才能再往里面放一个元素！
 * put、take
 *
 * @author Rem
 * @date 2021-03-09
 */

public class SynchronousQueueDemo {

    public static void main(String[] args) throws InterruptedException {


        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                blockingQueue.put("123");
                TimeUnit.SECONDS.sleep(3);
                blockingQueue.put("456");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, "A").start();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + blockingQueue.take());
                System.out.println(Thread.currentThread().getName() + blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "B").start();


    }
}
